package com.five.community.controller;

import com.five.community.bean.Parking;
import com.five.community.bean.Parkinguse;
import com.five.community.bean.Person;
import com.five.community.bean.Village;
import com.five.community.bean.vo.ParkingVo;
import com.five.community.bean.vo.ParkinguseVo;
import com.five.community.service.ParkingService;
import com.five.community.service.ParkinguseService;
import com.five.community.service.PersonService;
import com.five.community.service.VillageService;
import com.five.community.util.FileUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*
    车位使用管理控制器
 */
@Controller
@RequestMapping("/parkinguse")
public class ParkinguseController {

    @Autowired
    private ParkingService parkingService;
    @Autowired
    private ParkinguseService parkinguseService;
    @Autowired
    private VillageService villageService;
    @Autowired
    private PersonService personService;

    // 显示车位使用管理列表画面
    @RequestMapping("/listUi")
    public String listUi(ModelMap map, Integer pageNum) {

        if (pageNum == null) {
            pageNum = 1;
        }

        List<ParkinguseVo> parkinguseTotal = null;
        try {
            parkinguseTotal = parkinguseService.selectAllAndPnameHnameVname();
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.addAttribute("parkinguseTotal",parkinguseTotal);

        try {
            //初始化分页插件模型
            PageHelper.startPage(pageNum, 5);
            //查询所有信息获得列表集合
            //调用业务层查询全部车位使用信息、车位名称、持有人姓名和小区名称
            List<ParkinguseVo> parkinguses = parkinguseService.selectAllAndPnameHnameVname();
            //分页模型加入数据
            PageInfo<ParkinguseVo> pageInfo = new PageInfo<>(parkinguses);
            map.addAttribute("pageInfo", pageInfo);
            map.addAttribute("parkinguses", parkinguses);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "parkinguse_list";
    }

    // 根据小区名称查询全部信息
    @RequestMapping("/select")
    public String select(String vname, ModelMap map, Integer pageNum){

        System.out.println("vname = " + vname);

        if (pageNum == null) {
            pageNum = 1;
        }

        List<ParkinguseVo> parkinguseTotal = null;
        try {
            parkinguseTotal = parkinguseService.selectAllAndPnameHnameVname();
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.addAttribute("parkinguseTotal",parkinguseTotal);

        if (vname == null || "".equals(vname)) {
            return "redirect:/parkinguse/listUi";
        }
        else {
            try {
                //初始化分页插件模型
                PageHelper.startPage(pageNum, 5);
                //查询所有信息获得列表集合
                //调用业务层小区名称查询全部信息(车位使用信息、车位名称、持有人姓名和小区名称)
                List<ParkinguseVo> parkinguses = parkinguseService.selectAllAndPnameHnameVnameByVname(vname);
                //分页模型加入数据
                PageInfo<ParkinguseVo> pageInfo = new PageInfo<>(parkinguses);
                map.addAttribute("pageInfo", pageInfo);
                map.addAttribute("parkinguses", parkinguses);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return "parkinguse_list";
        }
    }

    // 显示添加车位使用数据画面
    @RequestMapping("/addUi")
    public String addUi(ModelMap map1, ModelMap map2, ModelMap map3){

        List<Village> villages = null;
        try {
            villages = villageService.selectAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        map1.addAttribute("villages",villages);

        List<Parking> parking = null;
        List<Parking> parkings = new ArrayList<>();
        try {
            parking = parkingService.selectAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
//        System.out.println(parking);
        for (Parking parks : parking) {		//foreach循环遍历集合中的元素
           if(parks.isState()){
               parkings.add(parks);
           }
        }
        System.out.println(parkings);
        map2.addAttribute("parkings", parkings);

        List<Person> persons = null;
        try {
            persons = personService.selectAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        map3.addAttribute("persons", persons);
        return "parkinguse_add";
    }

    // 添加车位使用数据
    @RequestMapping("/add")
    public String add(ParkinguseVo parkinguseVo, MultipartFile photos){
        long id1 = Long.parseLong(parkinguseVo.getVname());
        long id2 = Long.parseLong(parkinguseVo.getPname());
        long id3 = Long.parseLong(parkinguseVo.getHname());
        String fileName = "";
        try {
            fileName = FileUtils.fileUpload(photos);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 设置数据
        parkinguseVo.setPicture(fileName);
        parkinguseVo.setStartDate(new Date());
        parkinguseVo.setCreateDate(new Date());
        try {
            Parkinguse parkinguse = new Parkinguse(parkinguseVo.getId(),parkinguseVo.getCarNum(),fileName,id3,
                    parkinguseVo.getTelephone(),parkinguseVo.isType(),parkinguseVo.getCost(),
                    new Date(),parkinguseVo.getEndDate(),new Date(),id1,id2);
            parkinguseService.insert(parkinguse);
            parkingService.updateStateByPid(id2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/parkinguse/addUi";
    }

    // 根据编号删除车位使用数据
    @RequestMapping("/delete")
    public String delete(long id){
        try {
            parkinguseService.deleteById(id);
            parkingService.updateStateByPid2(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/parkinguse/listUi";
    }

    // 显示编辑车位使用数据画面
    @RequestMapping("/editUi")
    public String editUi(long id, ModelMap map){

        System.out.println("id = " + id);
        List<Parking> parkings = new ArrayList<>();
        try {
            Parkinguse parkinguse = parkinguseService.selectById(id);
            List<Village> villages = villageService.selectAll();
            List<Parking> parking = parkingService.selectAll();
            List<Person> persons = personService.selectAll();
            long pid = parkinguseService.selectPidById(id);
            System.out.println("pid = " + pid);

            for (Parking parks : parking) {		//foreach循环遍历集合中的元素
                if(parks.isState() || pid == parks.getId()){
                    parkings.add(parks);
                    System.out.println("parks.getId() = " + parks.getId());
                }
            }

            System.out.println(parkings);
            map.addAttribute("parkinguse", parkinguse);
            map.addAttribute("villages",villages);
            map.addAttribute("parkings",parkings);
            map.addAttribute("persons",persons);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "parkinguse_edit";
    }

    // 编辑车位使用数据
    @RequestMapping("/edit")
    public String edit(long id, ParkinguseVo parkinguseVo, MultipartFile photos) {

        long id4 = Long.parseLong(parkinguseVo.getVname());
        long id5 = Long.parseLong(parkinguseVo.getPname());
        long id6 = Long.parseLong(parkinguseVo.getHname());

        String fileName = "";
        try {
            fileName = FileUtils.fileUpload(photos);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 设置数据
        parkinguseVo.setPicture(fileName);
        try {
            Parkinguse parkinguse = new Parkinguse(parkinguseVo.getId(),parkinguseVo.getCarNum(),fileName,id6,
                    parkinguseVo.getTelephone(),parkinguseVo.isType(),parkinguseVo.getCost(),
                    parkinguseVo.getEndDate(),id4,id5);
            long pid = parkinguseService.selectPidById(id);
            System.out.println("parkinguse = " + parkinguse);
            System.out.println("id = " + id);
            System.out.println("pid = " + pid);
            System.out.println("id5 = " + id5);
            parkinguseService.updateById(parkinguse);

            if(id5 != pid){
                parkingService.updateStateByPid2(pid);
                parkingService.updateStateByPid(id5);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "parkinguse_edit";
    }


}
